/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
//just for testing gl stuff
#include <stdio.h>
#include <stdlib.h>

#include "GL/glut.h"

#include "controls.hpp"
#include "objects.hpp"

#include "ownutils.h"


CGLPickingContext tc;

clickable button1, button2;
draggable piece1;

C2DTextButton button3, button4, button5;

/******************************************************************************/
//functions for drawing stuff

//function for drawing text
void drawText(float x, float y, void *font, const char *string)
{
  const char *c;
  glRasterPos2f(x,y);
  for (c=string; *c != '\0'; c++)
  {
    glutBitmapCharacter(font, *c);
  }
}

/******************************************************************************/
//glut functions
void glutDisplayFunc()
{
  double m[16];
  //tc.Render();
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  
  glGetDoublev(GL_MODELVIEW_MATRIX,m);
  tc.SetModelviewMatrix(m);
  
  button1.Draw();
  button2.Draw();
  piece1.Draw();
  button3.Draw();
  button4.Draw();
  button5.Draw();
  
  glutSwapBuffers();
}

void glutReshapeFunc(int w, int h)
{
  double m[16];
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glViewport(0,0,w,h);
  gluOrtho2D(0,w,0,h);
  glGetDoublev(GL_PROJECTION_MATRIX,m);
  tc.SetProjectionMatrix(m);
  
  //tc.Resize(w,h);
  
  
}

void glutMouseFunc(int button, int state, int x, int y)
{
  if (state==GLUT_DOWN)
  {
    tc.OnMouseDown(x,y,1);
  } else if (state == GLUT_UP) {
    tc.OnMouseUp(x,y);
  }
    
}

void glutMotionFunc(int x, int y)
{
  tc.OnMouseMove(x,y);
}

void glutPassiveMotionFunc(int x, int y)
{
  tc.OnMouseMove(x,y);
}

void glutIdle()
{
  ownSleep_ms(50);
  glutPostRedisplay();

}

/******************************************************************************/
int main(int argc, char *argv[]) 
{
  glutInit(&argc,argv);
  
  printf("picking test\n");
  
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
  
  button1.x=100;
  button1.y=100;
  button2.x=200;
  button2.y=100;
  piece1.x=200;
  piece1.y=200;
  piece1.OldX=200;
  piece1.OldY=200;
  
  button3.SetPosition(200,250,0);
  button3.SetText("nappula");
  button3.Enable(false);
  button4.SetPosition(210,280,0);
  button4.SetText("ok");
  button5.SetPosition(210,330,0);
  button5.SetText("nappi omg!");
  
  tc.AddPickable(&button1);
  tc.AddPickable(&button2);
  tc.AddPickable(&piece1);
  tc.AddPickable(&button3);
  tc.AddPickable(&button4);
  tc.AddPickable(&button5);
  
  glClearColor(0.5, 0.5, 0.0, 0.0);
  
  glutCreateWindow("picking test");
  
  
  glutDisplayFunc(glutDisplayFunc);
  glutReshapeFunc(glutReshapeFunc);
  glutIdleFunc(glutIdle);
  glutMouseFunc(glutMouseFunc);
  glutMotionFunc(glutMotionFunc);
  glutPassiveMotionFunc(glutPassiveMotionFunc);
  glutMainLoop();
  
  return 0;
}
